﻿

CREATE PROCEDURE [dbo].[CalcDisponibilOrd]  @IDClasificare int, @Analitic TipCont,@PanaLa DateTime,@CodDoc int,@CodUnitate smallint,
	@s940 DECIMAL(20,2) OUT, @s944 DECIMAL(20,2) OUT, @s950 DECIMAL(20,2) OUT, @s960 DECIMAL(20,2) OUT AS 
begin
	DECLARE @strEroare AS varchar(200)
	DECLARE @totDebit AS DECIMAL(20,2)
	DECLARE @totCredit AS DECIMAL(20,2)
	DECLARE @totDebitPla AS DECIMAL(20,2)
	DECLARE @totCreditPla AS DECIMAL(20,2)
	DECLARE @totOrdonantat AS DECIMAL(20,2)

	DECLARE @cont TipCont


	SET @cont ='8060.'+@Analitic
	SELECT @totDebit=coalesce(SUM(ValoareLei),0) FROM [Rulaje] WHERE (IDDocument IN (SELECT IDDocument FROM RulajeDoc WHERE
		IDJurnal = @IDClasificare AND [CodUnitate] = @CodUnitate)) AND ContDebit= @cont
	SELECT @totCredit=coalesce(SUM(ValoareLei),0) FROM [Rulaje] WHERE (IDDocument IN (SELECT IDDocument FROM RulajeDoc WHERE
		 IDJurnal = @IDClasificare AND [CodUnitate] = @CodUnitate)) AND ContCredit=@cont 
	SET @s940 = @totDebit - @totCredit

	SET @cont ='8062.'+@Analitic
	SELECT @totDebit=coalesce(SUM(ValoareLei),0) FROM Rulaje WHERE  IDDocument IN (SELECT IDDocument FROM RulajeDoc d WHERE
		IDJurnal = @IDClasificare AND [CodUnitate] = @CodUnitate AND (d.DataDoc<=@PanaLa) ) AND ContDebit=@cont
	SELECT @totCredit=coalesce(SUM(ValoareLei),0) FROM Rulaje WHERE IDDocument IN (SELECT IDDocument FROM RulajeDoc d WHERE
		IDJurnal = @IDClasificare  AND [CodUnitate] = @CodUnitate AND (d.DataDoc<=@PanaLa) ) AND ContCredit=@cont
--	PRINT (@totDebit)
--	PRINT (@totCredit)

	SET @cont ='7701.'+@Analitic
	SELECT @totDebitPla=coalesce(SUM(ValoareLei),0) FROM [Rulaje] r WHERE (IDDocument IN (SELECT IDDocument FROM RulajeDoc WHERE
		IDJurnal = @IDClasificare AND [CodUnitate] = @CodUnitate AND DataDoc<@PanaLa )) AND ContDebit= @cont AND r.TipDocJust<>'IC'
	--SELECT coalesce(SUM(ValoareLei),0),TipDocJust FROM [Rulaje] r WHERE (IDDocument IN (SELECT IDDocument FROM RulajeDoc WHERE
	--	IDJurnal = @IDClasificare)) AND ContDebit= @cont AND r.TipDocJust<>'IC' GROUP BY TipDocJust

	SELECT @totCreditPla=coalesce(SUM(ValoareLei),0) FROM [Rulaje] r WHERE (IDDocument IN (SELECT IDDocument FROM RulajeDoc WHERE
		 IDJurnal = @IDClasificare AND [CodUnitate] = @CodUnitate AND DataDoc<@PanaLa)) AND ContCredit=@cont AND r.TipDocJust<>'IC'
	--SELECT coalesce(SUM(ValoareLei),0),TipDocJust FROM [Rulaje] r WHERE (IDDocument IN (SELECT IDDocument FROM RulajeDoc WHERE
		-- IDJurnal = @IDClasificare AND DataDoc<cast (@PanaLa as datetime))) AND ContCredit=@cont AND r.TipDocJust<>'IC' GROUP BY TipDocJust

--    PRINT (@totDebitPla)
--	PRINT (@totCreditPla)

	SELECT @totOrdonantat=coalesce(SUM(ValoareLei),0) FROM [Ordonantari] WHERE  (DataDoc<@PanaLa 
			OR (DataDoc=@PanaLa  AND IDOrdonantare<@CodDoc)) AND ArtAl Like @Analitic+'%'
			AND IDJurnal=@IDClasificare AND [CodUnitate] = @CodUnitate AND Achitat=0	
		
	SET @s944 = @totDebit + @totDebitPla - @totCredit - @totCreditPla - @totOrdonantat
    --PRINT (@totOrdonantat)



	SET @cont ='8066.'+@Analitic
	SELECT @totDebit=coalesce(SUM(ValoareLei),0) FROM Rulaje WHERE  IDDocument IN (SELECT IDDocument FROM RulajeDoc d WHERE
		IDJurnal = @IDClasificare AND [CodUnitate] = @CodUnitate AND (d.DataDoc<cast (@PanaLa as datetime) or ( d.DataDoc=cast(@PanaLa as datetime) AND (IDDocument<@CodDoc OR @CodDoc=0) )) ) AND ContDebit=@cont
	SELECT @totCredit=coalesce(SUM(ValoareLei),0) FROM Rulaje WHERE IDDocument IN (SELECT IDDocument FROM RulajeDoc d WHERE
		IDJurnal = @IDClasificare AND [CodUnitate] = @CodUnitate AND (d.DataDoc<cast (@PanaLa as datetime) or ( d.DataDoc=cast(@PanaLa as datetime) AND (IDDocument<@CodDoc OR @CodDoc=0) )) ) AND ContCredit=@cont
	SET @s950 =  @totDebit - @totCredit


	SET @cont ='8067.'+@Analitic
	SELECT @totDebit=coalesce(SUM(ValoareLei),0) FROM [Rulaje] WHERE (IDDocument IN (SELECT IDDocument FROM RulajeDoc WHERE
		IDJurnal = @IDClasificare AND [CodUnitate] = @CodUnitate)) AND ContDebit= @cont
	SELECT @totCredit=coalesce(SUM(ValoareLei),0) FROM [Rulaje] WHERE (IDDocument IN (SELECT IDDocument FROM RulajeDoc WHERE
		 IDJurnal = @IDClasificare AND [CodUnitate] = @CodUnitate)) AND ContCredit=@cont 
	SET @s960 = @totDebit - @totCredit

	 GOTO Gata
Eroare:
	 RAISERROR (@strEroare,16,-1)
Gata:
end